//
//  GTSearchResultSegment.m
//  GiftTalk
//
//  Created by Wan on 16/3/27.
//  Copyright © 2016年 Wan. All rights reserved.
//  搜索结果视图

#import "GTSearchResultSegment.h"

@interface GTSearchResultSegment()

/** 标签栏当前选中的按钮 */
@property (nonatomic,weak) UIButton *currentSelectedButton;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *indicatorViewLeading;
@property (weak, nonatomic) IBOutlet UIButton *leftButton;
@property (weak, nonatomic) IBOutlet UIButton *rightButton;
@property (weak, nonatomic) IBOutlet UIView *indicatorView;

- (IBAction)leftButtonOnClick:(UIButton *)sender;
- (IBAction)rightButtonOnClick:(UIButton *)sender;

@end

@implementation GTSearchResultSegment
- (void)awakeFromNib
{
    self.leftButton.selected = YES;
}

#pragma mark - 初始化 segment
+ (instancetype)segment
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}

- (void)leftButtonAddTarget:(id) target selector:(SEL)action forContrlEvents:(UIControlEvents)controlEvents
{
    [self.leftButton addTarget:target action:action forControlEvents:controlEvents];
}

- (void)rightButtonAddTarget:(id) target selecror:(SEL)action forContrlEvents:(UIControlEvents)controlEvents
{
    [self.rightButton addTarget:target action:action forControlEvents:controlEvents];
}

- (IBAction)leftButtonOnClick:(UIButton *)sender {
    if (sender.selected) return;
    
    self.rightButton.selected = !self.rightButton.selected;
    self.leftButton.selected = !sender.selected;
    self.indicatorViewLeading.constant = 0;
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (IBAction)rightButtonOnClick:(UIButton *)sender {
    if (sender.selected) return;
    
    self.leftButton.selected = !self.leftButton.selected;
    self.rightButton.selected = !sender.selected;
    self.indicatorViewLeading.constant = sender.width;
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

#pragma mark - 标题赋值
- (void)setLeftTitle:(NSString *)leftTitle
{
    _leftTitle = [leftTitle copy];
    
    [self.leftButton setTitle:leftTitle forState:UIControlStateNormal];
//    [self.leftButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
}

- (void)setRightTitle:(NSString *)rightTitle
{
    _rightTitle = [rightTitle copy];
    
    [self.rightButton setTitle:rightTitle forState:UIControlStateNormal];
//    [self.rightButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
}
@end
